<?php
declare (strict_types = 1);

namespace app\controller\api\v1\user\expert;

use app\model\communal\UserExpertEvaluateModel;
use app\service\logic\common\user\ExpertEvaluateService;
use app\validate\common\user\ExpertEvaluateValidate;
use think\App;
use think\Request;
use think\Response;
use think\response\Json;
use think\db\exception\DbException;
use app\controller\backend\BackendController;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class EvaluateController extends BackendController
{
    protected $service;

    public function __construct(App $app, ExpertEvaluateService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 显示资源列表
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);
        $this->validate($params, ExpertEvaluateValidate::class . '.index');
        $search['expert_id'] = $params['id'] ?? '';

        $fun = function ($model) {
            if (1 == $model['is_incognito']) {
                $model['user_name'] = '匿名用户';
            } else {
                $model['user_name'] = empty($model['user_name']) ? '**' : mb_substr($model['user_name'], 0, 1) . '**';
            }
            unset($model['user']);
            return $model;
        };
        $result = $this->service->getList($params, $search_fields, $search, ['user'], $fun);
        return amis($result);
    }

    /**
     * 保存新建的资源
     *
     * @param Request $request
     * @return Response
     */
    public function save(Request $request): Response
    {
        $params = $request->only(['id'] + $this->service->getSaveColumn());
        $params['user_id'] = $request->middleware('user')['id'];
        $params['expert_id'] = $params['id'] ?? '';
        unset($params['id']);
        $this->validate($params, ExpertEvaluateValidate::class . '.save');

        $result = $this->service->create($params);
        return amis($result);
    }

    /**
     * 显示资源
     *
     * @param $id
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function read($id): Json
    {
        $result = $this->service->getSimpleDetail($id);
        return amis($result);
    }

    /**
     * 单项修改
     *
     * @param Request $request
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function item(Request $request): Json
    {
        $params = $request->only($this->service->getItemColumn());
        $this->validate($params, ExpertEvaluateValidate::class . '.item');

        $result = $this->service->item($params);

        if ($result) {
            return amis();
        } else {
            return amis([], '操作失败', 417);
        }
    }

    /**
     * 筛选-满意内容
     *
     * @return Json
     */
    public function satisfy()
    {
        $options = $this->service->amisOptions(UserExpertEvaluateModel::$satisfyOptions, 'name', 'name');
        return amis(['options' => $options]);
    }

    /**
     * 筛选-满意内容(包含数量)
     *
     * @return Json
     */
    public function satisfies()
    {
        $options = $this->service->satisfies();
        return amis(['options' => $options]);
    }
}
